package com.ptswitch.material.pages.contract;

import java.util.Date;
import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.tapestry5.util.ValueLabel;
import org.hxzon.util.DateFormatUtil;
import org.hxzon.util.EMailUtil;
import org.hxzon.util.EMailUtil.EMail;

import com.ptswitch.material.model.common.Uzer;
import com.ptswitch.material.model.contract.Contract;
import com.ptswitch.material.pages.base.MaterialBasePage;
import com.ptswitch.material.service.ContractService;
import com.ptswitch.material.service.ContractSupplierService;
import com.ptswitch.material.service.UserService;
import com.ptswitch.material.util.HlogConstants;
import com.ptswitch.material.util.MaterialUtil;

public class CreateContract extends MaterialBasePage {

    @Inject
    private UserService userSer;
    @Inject
    private ContractService contractSer;
    @Inject
    private ContractSupplierService contractSupplierSer;
    @Property
    private Contract contract;
    @Property
    private List<Uzer> financials;
    @Property
    private List<Uzer> productions;
    @Property
    private List<Uzer> auditors;
    @Property
    private ValueLabel<Uzer> uzerLabel = new ValueLabel<Uzer>() {
        public String toLabel(Uzer value, int index) {
            return value.getRealname();
        }
    };
    //
    @InjectPage
    private ViewContract viewContract;

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init() {
        checkUserIsBuyer();
        contract = new Contract();
        String code = DateFormatUtil.format(new Date(), "'CON-'yyMMdd");
        code = contractSer.emptyCode(code);
        contract.setCode(code);
        financials = userSer.findByDepartment(Uzer.Department_Financial);
        productions = userSer.findByDepartment(Uzer.Department_Production);
        auditors = userSer.findByDepartment(Uzer.Department_ContractAuditor);
        return null;
    }

    @OnEvent(value = EventConstants.PROVIDE_COMPLETIONS, component = "c_supplier")
    List<String> provideCompletionsForContractSupplierName(String partial) {
        return contractSupplierSer.findAllName();
    }

    @OnEvent(value = EventConstants.SUCCESS)
    Object createContract() {
        checkUserIsBuyer();
        contract.setCreater(getVisitor());
        contract.setCreateTime(DateFormatUtil.formatToUniteTime(new Date()));
        contractSer.save(contract);
        //send email,now node
        EMail email = getMaterialUtil().newEMail();
        email.addTo(contract.getCreater().getEmail());
        email.addTo(contract.getFinancial().getEmail());
        email.addTo(contract.getProduction().getEmail());
        email.addTo(contract.getAuditor().getEmail());
        email.setSubject(contract.getCreater().getRealname() + "新建了合同：" + contract.getCode());
        email.addHtml(MaterialUtil.linkForViewContract(getWebUrl(), contract));
        EMailUtil.send(email);
        //hlog
        hlog(HlogConstants.addContract, contract);
        return viewContract.initialize(contract.getId());
    }

}
